package com.huluobo.h3kotlinproject.detail

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.huluobo.h3kotlinproject.net.Const
import okhttp3.MediaType
import okhttp3.RequestBody

/**
 *  Created by LC on 2023/10/24.
 */
class DetailViewModel(application: Application) : AndroidViewModel(application) {
    private val goodsId = MutableLiveData<Int>()
    private val repo = DetailRepo()

    val detailSuccess = MutableLiveData<GoodsDetailBeanResult>()
    val detailFailed = MutableLiveData<String>()

    val addToCartSuccess = MutableLiveData<ChangeCartBeanResult>()
    val addToCartFailed = MutableLiveData<String>()

    fun setGoodsId(id: Int) {
        goodsId.value = id
    }

    fun getGoodsId(): LiveData<Int> {
        return goodsId
    }

    fun getGoodsDetail(id: Int) {
        repo.getGoodsDetail(id, detailSuccess, detailFailed)
    }

    fun addToCart(count: Int, goods_id: Int) {
        val map = mutableMapOf<String, Int>()
        map[Const.PARAM_GOODS_ID] = goods_id
        map[Const.PARAM_COUNT] = count
        val json = Gson().toJson(map)
        val body = RequestBody.create(MediaType.parse(Const.MEDIA_PARSE), json)
        repo.addToCart(body, addToCartSuccess, addToCartFailed)
    }
}